package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysGoods;
import com.ruoyi.project.system.domain.SysGoodsFile;
import com.ruoyi.project.system.service.ISysGoodsFileService;
import com.ruoyi.project.system.service.ISysGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mobileUser/goods/")
public class UserGoodsController extends BaseController {

    @Autowired
    private ISysGoodsService sysGoodsService;

    @Autowired
    private ISysGoodsFileService sysGoodsFileService;

    /**
     * 用户查询二手物品列表  不登录可以查看
     */
    @SignatureValidation
    @GetMapping("list")
    public AjaxResult list(SysGoods sysGoods)
    {
        startPage();
        List<SysGoods> list = sysGoodsService.selectUserGoods(sysGoods);
        if(sysGoods.getGoodsId() != null){
            List<SysGoodsFile> sysGoodsFiles = sysGoodsFileService.queryGoodsFile(sysGoods.getGoodsId());
            sysGoodsService.updatePageViews(sysGoods);
            Map<String,Object> map = new HashMap();
            map.put("sysGoodsFiles",sysGoodsFiles);
            map.put("list",list);
            return AjaxResult.success(map);
        }
        return AjaxResult.success(list);
    }

    /**
     * 登陆查看
     */
    @SignatureValidation
    @GetMapping("NotList")
    public AjaxResult NotPasslist(SysGoods sysGoods)
    {
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysGoods.setMobileUserId(mobileUserId);
        List<SysGoods> list = sysGoodsService.selectSysGoodsList(sysGoods);
        return AjaxResult.success(list);
    }
    /**
     * 新增二手物品
     * 登录查看  不可重复提交
     */
    @Log(title = "二手物品", businessType = BusinessType.INSERT)
    @PostMapping("addGoods")
    @SignatureValidation
    @RepeatSubmit
    public AjaxResult add(@RequestBody SysGoods sysGoods)
    {
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        SysGoodsFile sysGoodsFile = new SysGoodsFile();
        sysGoods.setMobileUserId(mobileUserId);
        sysGoods.setExamineStatus("0");
        sysGoodsService.insertSysGoods(sysGoods);
        List<String> urls = sysGoods.getUrls();
        for (int i = 0; i < urls.size() ; i++) {
            String url = urls.get(i);
            sysGoodsFile.setGoodsId(sysGoods.getGoodsId());
            sysGoodsFile.setUrl(url);
            sysGoodsFileService.insertSysGoodsFile(sysGoodsFile);
        }
        return AjaxResult.success("已提交,待审核.....");
    }

    /**
     * 更改物品状态  登录之后显示
     * 传物品ID  传修改状态  必传
     * */
    @PostMapping("updateStatus")
    public AjaxResult updateStatus(@RequestBody SysGoods sysGoods){
        SysGoods sysGoods1 = sysGoodsService.selectSysGoodsById(sysGoods.getGoodsId());
        String examineStatus = sysGoods1.getExamineStatus();
        if(!examineStatus.equals("1")){
            return AjaxResult.error("该物品审核有问题");
        }
        return AjaxResult.success(sysGoodsService.updateItemStatus(sysGoods));
    }
}
